<!DOCTYPE html PUBLIC "-//IETF//DTD HTML 2.0//EN">
<?php


/*
 *  A Part of snock project
 *   Author king-of-war
 * Last modified 2009-5-12
 *  designed for sort operation
 */
class company {
	private $root_path;

	function __construct($url) {
		/*
		* load connection class
		*/
		include_once $url . 'includes/conn.php';
		/*
		 * load xml class
		 */
		include_once $url . 'includes/xml/xml_op.php';
		$this->set_root_path($url);

	}
	/*
	 * select the date from database
	 */
	function company_get() {
		$conn = new conn();
		$sql = 'select * from company';
		$conn->set_sql($sql);
		$line = $conn->get_line();
		$company = array ();
		if ($line) {
			$result = $conn->get_result();
			$row = $result->fetch_assoc();
			$company['name'] = $row['name'];
			$company['notice'] = $row['notice'];
			$company['phone'] = $row['phone'];
			$company['zip_code'] = $row['zip_code'];
			$company['linkman'] = $row['linkman'];
			$company['mail'] = $row['mail'];
			$company['address'] = $row['address'];
			$company['icp'] = $row['icp'];

		} else {
			echo 'Error: Config data lost';
			//	exit;
		}

		return $company;
	}
	/*
	 * update site config data
	 */
	function company_updata($name,$notice, $phone, $zip_code, $linkman, $mail, $address,$icp) {
		$conn = new conn();
		$line = 0;
		$sql = sprintf("update company set name='%s',notice='%s',phone='%s',zip_code='%s',linkman='%s',mail='%s',address='%s',icp='%s'", $name,$notice, $phone, $zip_code, $linkman, $mail, $address,$icp);
		$conn->set_sql($sql);
		$line = $conn->get_line();
		if ($line) {
			$this->xml_write($name,$notice, $phone, $zip_code, $linkman, $mail, $address,$icp);
		}
		return $line;
	}
	/*
	 * write to xml
	 */
	function xml_write($name,$notice, $phone, $zip_code, $linkman, $mail, $address,$icp) {

		$url = $this->get_root_path();
		$xml_op = new xml_op($url);
		$file = 'includes/xml/company.xml';
		if (file_exists($url . $file)) {
			$xml = simplexml_load_file($url . $file);
			/*
			 * write to xml
			 */
			$xml->company[0]->name = $name;
			$xml->company[0]->notice = $notice;
			$xml->company[0]->phone = $phone;
			$xml->company[0]->zip_code = $zip_code;
			$xml->company[0]->linkman = $linkman;
			$xml->company[0]->mail = $mail;
			$xml->company[0]->address = $address;
			$xml->company[0]->icp = $icp;
			$contant = $xml->asXML();
			$xml_op->xml_creat($file, $contant);
			return true;
		} else {
			echo 'File open error <br/>Check the file please';
			return false;
			exit;
		}

	}

	/*
	 * read xml
	 */
	function xml_read($key) {

		$url = $this->get_root_path();
		$xml_op = new xml_op($url);
		$file = 'includes/xml/company.xml';
		if (file_exists($url . $file)) {

			$xml = simplexml_load_file($url . $file);

			/*
			 * read xml
			 */

			$result = $xml->company[0]-> $key;

			return $result;
		} else {
			echo 'File open error <br/>Check the file please';
			return null;
			exit;
		}

	}

	/*
	 * function for vars which used above
	 */
	function set_root_path($url) {
		$this->root_path = $url;
	}
	function get_root_path() {
		return $this->root_path;
	}

}
?>
